package com.yxym.homework;

import java.util.ArrayList;
import java.util.Comparator;

@SuppressWarnings({"all"})
public class Exercise01 {
    public static void main(String[] args) {
        ArrayList<Employee> employees = new ArrayList<>();
        employees.add(new Employee("小白",50000,new MyData(2000,2,9)));
        employees.add(new Employee("小黑",30000,new MyData(2001,5,1)));
        employees.add(new Employee("小青",40000,new MyData(2002,4,5)));
        System.out.println(employees);

        employees.sort(new Comparator<Employee>() {
            @Override
            public int compare(Employee employee1, Employee employee2) {

                //对传入的参数进行验证
                if (!(employee1 instanceof Employee && employee2 instanceof Employee)){
                    System.out.println("类型不匹配...");
                    return 0;
                }

                //比较name
                int i = employee1.getName().compareTo(employee2.getName());
                if(i != 0){
                    return i;
                }

                //name相同下比较bir
                return employee1.getBir().compareTo(employee2.getBir());
            }
        });
        System.out.println("================");
        System.out.println(employees);
    }
}

class Employee{
    private String name;
    private double sal;
    private MyData bir;

    public Employee(String name, double sal, MyData bir) {
        this.name = name;
        this.sal = sal;
        this.bir = bir;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getSal() {
        return sal;
    }

    public void setSal(double sal) {
        this.sal = sal;
    }

    public MyData getBir() {
        return bir;
    }

    public void setBir(MyData bir) {
        this.bir = bir;
    }

    @Override
    public String toString() {
        return "\n"+"Employee{" +
                "name='" + name + '\'' +
                ", sal=" + sal +
                ", bir=" + bir +
                '}';
    }
}

class MyData implements Comparable<MyData>{
    private int year;
    private int month;
    private int day;

    public MyData(int year, int month, int day) {
        this.year = year;
        this.month = month;
        this.day = day;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public int getMonth() {
        return month;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public int getDay() {
        return day;
    }

    public void setDay(int day) {
        this.day = day;
    }

    @Override
    public String toString() {
        return "MyData{" +
                "year=" + year +
                ", month=" + month +
                ", day=" + day +
                '}';
    }

    @Override
    public int compareTo(MyData o) {//对年龄进行比较

        int yearMinus = year - o.getYear();
        if(yearMinus != 0){
            return yearMinus;
        }
        //year相同，就比较month
        int monthMinus = month - o.getMonth();
        if(monthMinus != 0){
            return monthMinus;
        }
        //month相同，就比较day
        return day - o.getDay();
    }
}